// 主界面
;define(function(require, exports, module) {
    'use strict';
    var util = require("util");
    var base = require("base");
    var leftMenu = require("../leftMenu/leftMenu");
    var topMenu = require("../topMenu/topMenu");
    var router = require("../hash/pageRouter");
    var footer = require("../footer/footer_main");
    var dialog = require("dialog");
    var validate = require("validate");
    var constant = require("constant");
    var tools = require("tools");
    var firstVisited = 0;
    var auditState;
    var leftM;
    var pageIndex = 1,
        pageSize = 10,
        totalPageCount = 1,
        totalRecordCount = 0; //分页初始化
        
    var init = function(){
    	firstVisited = base.getUrlParam("firstVisited");
    	auditState = base.getUrlParam("auditState");
    	leftM = base.getUrlParam("leftM");
    	leftM = leftM == null?4:leftM;
    	footer.init();
    	$("#footer").removeClass("none");
		
    	leftMenu.init(leftM);
    	
    	$(".memberList").parent().parents("li").addClass("leftMenu-open");
    	$(".leftMenu-item").removeClass("leftMenu-active");
    	$(".ModifyclientManage").addClass("leftMenu-active");
    	topMenu.init(2);
    	$(".footer").removeClass("none");
    	
    	if (auditState != null) {
    		$("select[name=auditState]").val(auditState);
    	}
    	getEnterpriseList();
		initEvent();
		router.init();
		
		if(firstVisited != 1){
			$(".table-section").removeClass("none");
		}
    };
    
    var html_delete = require("../client/clientManage_delete.html");
	var html_details = require("../client/clientManage_details.html");
	var initEvent = function(){
		//若未登录，则返回登陆页面
		if(base.getData("sid")==null||base.getData("sid")==""||base.getData("sid")==undefined){
			top.location.href="../login/login.html";
		}
		
		$(".memberGradeAduitList").unbind("click").click(function(){
			router.route("client/memberGradeaudit.html");
		});
    };
    
     //搜索按钮
    $(".table-search-button").on("click",function(){
        pageIndex = 1;
		pageSize = 10;
		totalPageCount = 1;
		totalRecordCount = 0; //分页初始化
        getEnterpriseList();
    });
    	  
    //获取企业用户列表
	var getEnterpriseList = function(){
		var body = base.parseForm($("#searchEnterprise"));
		body.createDateTimeBegin = body.createDateTimeBegin.split("-").join("");
		body.createDateTimeEnd = body.createDateTimeEnd.split("-").join("");
		
        base.fnAjax({
            action: "queryEnterpriseModifyPageList.json",
//        	action:"queryBankCode.json",
            oData: {
            	enterpriseType: 0,
            	pageIndex: pageIndex,
//            	dotName:body.dotName
                pageSize: pageSize,
                enterpriseName:body.enterpriseName,
                legalPerson:body.legalPerson,
                state:body.state,
                auditState:body.auditState,
                createDateTimeBegin:body.createDateTimeBegin,
                createDateTimeEnd:body.createDateTimeEnd
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	juicer.register("getValueByKey", constant.getValueByKey);
                	juicer.register("formatCurrency", tools.formatCurrency);
                	
                	var tpl = $("#enterpriseListTpl").html();
                    var html = juicer(tpl, data);
                    $("#enterpriseListBody").html(html);
                    totalPageCount = data.totalPageCount;
                    totalRecordCount = data.totalRecordCount;
                    base.pageControl(
                    	$("#clientPagenation"),
                        totalRecordCount,
                        data.pageIndex,
                        data.pageSize,
                        function(p) {
                            pageIndex = p;
                            getEnterpriseList();
                    });
                    
                     //点击详情
			        $("#enterpriseListBody").on("click",".table_details_link",function(){
			           var authenticationIds = [];
			           var $this = $(this).parents("tr.tr_style");
			           authenticationIds.push($this.attr("authenticationId"));
						router.route("client/modifyClienManage_details.html?authenticationId="+authenticationIds,true);
			        });							
					tools.inquireCurrentOperationList($("#main"));
					
                } else {
                    base.alert(2,"信息获取失败");
                }
            }
		});
	};
	
    return {
        init:init
    };
});